package com.todo.ln.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 1.@description:
 * 2.@author:ln
 * 3.@Date:2022/9/15
 */
@Configuration
// 配置跨域解决方法
public class CorsConfig {
    private CorsConfiguration buildConfig(){
        CorsConfiguration configuration = new CorsConfiguration();
        // 获取所有的请求头
        configuration.addAllowedHeader("*");
        // 获取所有的请求方法
        configuration.addAllowedMethod("*");
        // 获取所有的请求源
        configuration.addAllowedOrigin("*");
        return configuration;

    }
    @Bean
    public CorsFilter corsFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**",buildConfig());
        return new CorsFilter(source);
    }

}
